package com.backend.demo.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.stereotype.Controller;

import com.backend.demo.entities.Permission;
import com.backend.demo.service.IPermissionService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 权限管理
 * @author Administrator
 *
 */
@ParentPackage("user")
@Namespace("/jspView")
@InterceptorRefs(value = { @InterceptorRef(value = "loginStack") })
@Controller
public class PermissionAction extends ActionSupport implements RequestAware,
		ModelDriven<Permission> {
	
	private static final long serialVersionUID = 1L;
	
	private Map<String,Object> request;
	
	private Permission permission;
	
	@Resource(name="permissionService")
	private IPermissionService permissionService;

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public Permission getModel() {
		if(permission == null){
			permission = new Permission();
		}
		return permission;
	}
	
	@Action(value="getAllPermissions",results={@Result(name=SUCCESS,location="/jspView/permissions/permissionList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getAllPermissions(){
		List<Permission> permissions = permissionService.getAllPermissions();
		request.put("permissions", permissions);
		return SUCCESS;
	}
	
	@Action(value="getPermissionsByCondition",results={@Result(name=SUCCESS,location="/jspView/permissions/permissionList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getPermissionsByCondition(){
		List<Permission> permissions = permissionService.getPermissionsByCondition(permission);
		request.put("permissions", permissions);
		return SUCCESS;
	}
	
	@Action(value="getPermissionById",results={@Result(name=SUCCESS,location="/jspView/permissions/editPermission.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getPermissionById(){
		Permission permissionNew = permissionService.getPermissionById(permission.getPermissionID());
		if(permissionNew != null){
			request.put("permission", permissionNew);
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="addPermission",results={@Result(name=SUCCESS,location="/jspView/getAllPermissions"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String addPermission(){
		int num = permissionService.addPermission(permission);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="editPermission",results={@Result(name=SUCCESS,location="/jspView/getAllPermissions"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String editPermission(){
		int num = permissionService.editPermission(permission);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="deletePermission",results={@Result(name=SUCCESS,location="/jspView/getAllPermissions"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String deletePermission(){
		int num = permissionService.deletePermission(permission);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="redirectPermission",results={@Result(name=SUCCESS,location="/jspView/permissions/addPermission.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String redirectPermission(){
		return SUCCESS;
	}

}
